package com.huawei.petapp2.dialog;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.huawei.petapp2.R;
import com.huawei.petapp2.entity.GetOrderBean;
import com.huawei.petapp2.entity.ShopDetailBean;
import com.huawei.petapp2.utils.ParseUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 作者： njb
 * 时间： 2018/6/19 0019-上午 9:50
 * 描述： 添加购物车
 * 来源：
 */
public class AddCartDialog extends Dialog implements View.OnClickListener {
    private ShopDetailBean detailModel;
    private TextView clearTv;
    private ImageView addBtn;
    private ImageView minusBtn;
    private Button finishBtn;
    private EditText cartCount;

    private Activity mContext;
    private String content;
    private OnCloseListener listener;
    private String positiveName;
    private String negativeName;
    private String title;

    public AddCartDialog(Activity context, int alertDialogStyle, ShopDetailBean detailModel, OnCloseListener listener) {
        super(context,alertDialogStyle);
        this.mContext = context;
        this.detailModel = detailModel;
    }

    public AddCartDialog(Activity context, String content) {
        super(context, R.style.AlertDialogStyle);
        this.mContext = context;
        this.content = content;
    }

    public AddCartDialog(Activity context, int themeResId, String content) {
        super(context, themeResId);
        this.mContext = context;
        this.content = content;
    }

    public AddCartDialog(Activity context, int themeResId, String content, OnCloseListener listener) {
        super(context, themeResId);
        this.mContext = context;
        this.content = content;
        this.listener = listener;
    }


    protected AddCartDialog(Activity context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.mContext = context;
    }

    public com.huawei.petapp2.dialog.AddCartDialog setTitle(String title) {
        this.title = title;
        return this;
    }

    public com.huawei.petapp2.dialog.AddCartDialog setPositiveButton(String name) {
        this.positiveName = name;
        return this;
    }

    public com.huawei.petapp2.dialog.AddCartDialog setNegativeButton(String name) {
        this.negativeName = name;
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_add_cart);
        setCanceledOnTouchOutside(true);
        initView();
        Objects.requireNonNull(getWindow()).setWindowAnimations(R.style.AlertDialogStyle); //设置窗口弹出动画

        //全屏处理
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        WindowManager wm = mContext.getWindowManager();

        lp.width = wm.getDefaultDisplay().getWidth(); //设置宽度
        //lp.height = wm.getDefaultDisplay().getHeight();
        getWindow().setAttributes(lp);
        getWindow().setGravity(Gravity.BOTTOM);
    }

    public void initView() {
        clearTv = findViewById(R.id.clear_tv);
        addBtn = findViewById(R.id.add_count_btn);
        minusBtn = findViewById(R.id.minus_count_btn);
        finishBtn = findViewById(R.id.finish_btn);
        cartCount = findViewById(R.id.edit_count);
        addBtn.setOnClickListener(this);
        clearTv.setOnClickListener(this);
        minusBtn.setOnClickListener(this);
        finishBtn.setOnClickListener(this);
        cartCount.setOnClickListener(this);
        cartCount.setFocusableInTouchMode(true);



    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onClick(View v) {
        int count = ParseUtils.parseInt(cartCount.getText().toString());
        if (count <= 1) {
            cartCount.setClickable(false);
        }
        switch (v.getId()) {
            case R.id.clear_tv:
                cartCount.setText(count + "");
                break;
            case R.id.add_count_btn:
                count++;
                cartCount.setText(count + "");
                cartCount.setClickable(true);
                break;
            case R.id.minus_count_btn:
                if (count > 1) {
                    count--;
                    cartCount.setText(count + "");
                    if (count == 1) {
                        cartCount.setClickable(false);
                    }
                } else {
                    cartCount.setClickable(false);
                }
                break;
            case R.id.edit_count:
                if (listener != null) {
                    listener.onClick(this, true);
                }
                break;
            case R.id.finish_btn:
                GetOrderBean model = new GetOrderBean();
                List<ShopDetailBean> goodList = new ArrayList<>();
                detailModel.setGood_count(1);
                goodList.add(detailModel);
                model.setModel(goodList);
                this.dismiss();
                break;
        }
    }

    public interface OnCloseListener {
        void onClick(Dialog dialog, boolean confirm);
    }
}
